<?php namespace App\Http\Controllers\user\login;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Session;
use App\Models\Users;
use App\Models\Cellphone;
use App\Models\Email;
use Input;
use Redirect;

class IndexController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		return view('user.login.index');
	}

	public function dologin()
	{
		$username = trim(Input::get("username"));
		$password = trim(Input::get("password"));

		if(Session::has("errorTimes") && Session::get("errorTimes") >=3){
			$code = strtoupper(trim(Input::get("code")));
			if($code != Session::get("code")){
				return self::error(self::VALIDATA_CODE_IS_WRONG);
			}
		}

		$type_reg = '/^1[0-9]{10}$/';
		if(preg_match($type_reg, $username)){
			$user = Cellphone::where("cell_phone", $username);

			if($user->count() && $password == $user->get()[0]->password){
				$uid = $user->get()[0]->uid;
				$uSession = Users::with(['profile'])->where("id", $uid)->get()[0]->toArray();

				if($uSession["state"] == 1){
					return self::error(self::USER_IS_FREEZE);
				}
				if($uSession["state"] == 2){
					return self::error(self::USER_IS_LOCK);
				}
				Session::put("user", $uSession);
				Session::forget("errorTimes");

				return $this->json(["user" => $uSession]);
			}else{
				$times = Session::has('errorTimes') ? (int)Session::get('errorTimes') + 1 : 1;
				Session::put('errorTimes', $times);
				return self::error(self::USERNAME_PASSWORD_IS_WRONG);
			}
		}else{
			$user = Email::where("email", $username);
			if($user->count() && $password == $user->get()[0]->password){

				$userData = $user->get()[0]->toArray();
				$uid = $userData["uid"];
				if($userData["state"] == 0){
					return self::error(self::USER_IS_NOT_ACTIVATE);
				}
				$uSession = Users::with(['profile'])->where("id", $uid)->get()[0]->toArray();

				if($uSession["state"] == 1){
					return self::error(self::USER_IS_FREEZE);
				}
				if($uSession["state"] == 2){
					return self::error(self::USER_IS_LOCK);
				}

				Session::put("user", $uSession);
				Session::forget("errorTimes");

				return $this->json(["user" => $uSession]);
			}else{
				$times = Session::has('errorTimes') ? (int)Session::get('errorTimes') + 1 : 1;
				Session::put('errorTimes', $times);
				return self::error(self::USERNAME_PASSWORD_IS_WRONG);
			}
		}


	}



	public function doelogin()
	{
		$username = trim(Input::get('username'));
		$password = trim(Input::get('password'));

		if(Session::has('errorTimes') && Session::get('errorTimes') >= 3 ){
			$code = trim(Input::get('code'));
			if(strtoupper($code) != Session::get('code')){
				return self::error(self::VALIDATA_CODE_IS_WRONG);
			}
		}
		if(Session::has($username)){
			$se = Session::get($username);
			if( $se['indb'] == 1 && $password != $se['pw']  ){
				$times = Session::has('errorTimes') ? Session::get('errorTimes') + 1  : 1;
				Session::put('errorTimes', $times);
				$se['times']++;
				Session::put($username, $se);
				if(Session::get('errorTimes') >= 3){
					return self::error(self::VALIDATA_CODE);
				}
				return self::error(self::USERNAME_PASSWORD_IS_WRONG);
			}else if($se['indb'] == 0){
				$times = Session::has('errorTimes') ? Session::get('errorTimes') + 1  : 1;
				Session::put('errorTimes', $times);
				$se['times']++;
				Session::put($username, $se);
				if(Session::get('errorTimes') >= 3){
					return self::error(self::VALIDATA_CODE);
				}
				return self::error(self::USERNAME_PASSWORD_IS_WRONG);
			}
		}
		$type_reg = '/^1[0-9]{10}$/';
		if(preg_match($type_reg, $username)){
			$user = Cellphone::with('user')->where("cell_phone", $username)->get();
			// $temp = "tel:" + $username;
		}else{
			$user = Email::with('user')->where("email", $username)->get();
		}
		if($user->count()){
			if($password != $user[0]->password){
				if(!Session::has($username)){
					Session::put($username , array(
						'indb'		=>	1,
						'times' 	=> 	1,
						'freeze'	=>	$user[0]->user->state,
						'uid'		=>	$user[0]->uid,
						'pw'		=>	$user[0]->password,
						'freezeTime'=>	$user[0]->user->freeze_time,
						'startTime'	=>	$user[0]->user->freeze_duration
						));
					$times = Session::has('errorTimes') ? Session::get('errorTimes') + 1 : 1;
					Session::put('errorTimes', $times);
					return self::error(self::USERNAME_PASSWORD_IS_WRONG);
				}
			}
		}else{
			$times = Session::has('errorTimes') ? Session::get('errorTimes') + 1 : 1;
			Session::put('errorTimes', $times);
			if(Session::get('errorTimes') >= 3){
				return self::error(self::VALIDATA_CODE);
			}
			return self::error(self::USERNAME_PASSWORD_IS_WRONG);
		}
		Session::put('user', $user[0]->toArray());
		return $this->json([]);
	}

	// 退出登录
	public function loginOut()
	{
		if(Session::has('user')){
			Session::forget('user');
			return Redirect::to('login');
		}
	}

	// 后台用户列表

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}
}
